package com.spice.service.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spice.common.exception.SpiceException;
import com.spice.common.result.Response;
import com.spice.common.result.ResultCodeEnum;
import com.spice.common.util.CheckUtil;
import com.spice.service.entity.pojo.NutritionContain;
import com.spice.service.mapper.NutritionContainMapper;
import com.spice.service.service.NutritionContainService;
import com.spice.service.service.ShapeDescService;
import org.springframework.stereotype.Service;

@Service
public class NutritionContainServiceImpl extends ServiceImpl<NutritionContainMapper, NutritionContain>
        implements NutritionContainService {


    public Response saveNutritionContain(NutritionContain newNutritionContain) {
        //确认必要参数
        if (CheckUtil.objectsHasNull(
                newNutritionContain.getSourcename(),
                newNutritionContain.getPrincipal())) {
            return Response.setResult(ResultCodeEnum.PARAM_LACK);
        }
        //检查名称是否重复？
        if (this.count(new LambdaQueryWrapper<NutritionContain>().eq(NutritionContain::getSourcename, newNutritionContain.getSourcename())) > 0) {
            throw new SpiceException().message("该原料信息已存在");
        }
        this.save(newNutritionContain);
        return Response.ok().message("添加原料营养成分数据成功").data("data", newNutritionContain);
    }

    public Response removeNutritionContain(Long id) {
        // 确认必要参数
        if (CheckUtil.objectsHasNull(id)) {
            throw new SpiceException(ResultCodeEnum.PARAM_LACK);
        }
        //确认要删除的是否存在
        NutritionContain removedNutritionContain = this.getById(id);
        if (CheckUtil.objectsHasNull(removedNutritionContain)) {
            throw new SpiceException().message("该原料营养成分描述数据不存在");
        }
        this.removeById(id);
        return Response.ok().message("原料营养成分描述数据删除成功").data("data", removedNutritionContain);
    }

    public Response updateNutritionContain(Long id, NutritionContain newNutritionContain) {
        //确认必要参数
        if (CheckUtil.objectsHasNull(id, newNutritionContain)) {
            return Response.setResult(ResultCodeEnum.PARAM_LACK);
        } else {
            //确认待更新数据是否存在
            NutritionContain updatedNutritionContain = this.getById(id);
            if (CheckUtil.objectsHasNull(updatedNutritionContain)) {
                throw new SpiceException().message("该原料营养成分描述数据不存在");
            }
            //确认更新后的辣椒名称是否与已有的重名
            //更新后的名称与已有的不同的情况下
            if (!updatedNutritionContain.getSourcename().equals(newNutritionContain.getSourcename())) {
                if (this.count(new LambdaQueryWrapper<NutritionContain>().eq(NutritionContain::getSourcename, newNutritionContain.getSourcename())) > 0) {
                    throw new SpiceException().message("该原料信息已存在");
                }
            }
            //更新待更新的数据id和createTime
            newNutritionContain.setId(id);
            newNutritionContain.setCreateTime(updatedNutritionContain.getCreateTime());
            if (this.updateById(newNutritionContain)) {
                return Response.ok().message("原料营养成分描述数据更新成功").data("data", newNutritionContain);
            } else {
                return Response.error().message("原料营养成分描述数据更新失败");
            }
        }
    }
}
